package com.hmk.blogboot.web.admin;


import com.hmk.blogboot.po.Blog;
import com.hmk.blogboot.po.Type;
import com.hmk.blogboot.po.User;
import com.hmk.blogboot.service.BlogService;
import com.hmk.blogboot.service.TagService;
import com.hmk.blogboot.service.TypeService;
import com.hmk.blogboot.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/*控制器拦截 根据用户的身份*/
@Controller
@RequestMapping("/admin")
public class BlogController {

    //定义常量路径
    private static final String INPUT = "admin/blogs-input";
    private static final String LIST  = "admin/blogs";
    private static final String REDIRECT_LIST = "redirect:/admin/blogs";

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    //根据用户做个拦截 只有admin用户才能访问该blogs接口
    @GetMapping("/blogs")
    //分页参数设置 每页3条记录，updateTime排序，降序
    public String blogs(@PageableDefault(size = 3,sort={"updateTime"},direction = Sort.Direction.DESC)Pageable pageable,
                        BlogQuery blog, Model model){


        //模板调用addAttribute方法添加数据到model,渲染在模板页面
        //分类是一个下拉框，这里获取到所有的type类型表中所有类型数据
        model.addAttribute("types",typeService.listType());
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return LIST;
    }



    /*搜索，更新表格数据，上面的搜索里面内容关键词 保持不变*/
    @PostMapping("/blogs/search")
    //分页参数设置 每页3条记录，updateTime排序，降序
    public String search(@PageableDefault(size = 3,sort={"updateTime"},direction = Sort.Direction.DESC)Pageable pageable,
                                 BlogQuery blog, Model model){
        model.addAttribute("page",blogService.listBlog(pageable,blog));

        //blogList表示要刷新页面片段 这里指的是表格里面内容，在thymeleaf模板里面定义 blogList片段定义在表格位置
        return "admin/blogs :: blogList";
    }

    //blog-input页面 页面数据初始化
    @GetMapping("/blogs/input")
    public String input(Model model){
        //拿到标签和分类
       setTypeAndTag(model);
       //初始化blog
        model.addAttribute("blog",new Blog());
        return INPUT;

    }
    //拿到标签和分类
    private void setTypeAndTag(Model model){
        model.addAttribute("types", typeService.listType());
        model.addAttribute("tags", tagService.listTag());
    }

    /*新增和修改共用一个方法*/
    @PostMapping("blogs")
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session){
        //session中当前用户登录，user保存到session,从session会话中取出user,设置blog对象
        blog.setUser((User) session.getAttribute("user"));
        //blog对象设置type分类
        //新增blog 前端传来blog中new 带有typeId得type属性
        blog.setType(typeService.getType(blog.getType().getId()));
        //blog设置tags
        blog.setTags(tagService.listTag(blog.getTagIds()));

        //创建Blog类型b对象
        Blog b;
        if(blog.getId()==null){
            //前端传来blog对象没有id,数据库中没有blog记录
            //新增blog操作
            b = blogService.saveBlog(blog);
        }else{
            //前端传来带有blog id得blog记录，blog做修改操作
            b =blogService.updateBlog(blog.getId(),blog);
        }
        //做校验
        if(b==null){
            attributes.addFlashAttribute("message","操作失败");
        }else{
            attributes.addFlashAttribute("message","操作成功");
        }
        return REDIRECT_LIST;
    }





    /*修改之后跳转到新增页面*/
    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Long id, Model model){
       //博客类型types和tags标签显示在页面上面
        setTypeAndTag(model);

        //根据id获取blog
        Blog blog =blogService.getBlog(id);
        //获取到blog对象 将List类型tags,变成‘1,2,3’形式字符串
        blog.init();
       //根据id获取blog博客对象
        model.addAttribute("blog",blog);
        return INPUT;
    }

    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        //使用blogService接口调用deleteBlog(id) 根据id进行删除
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message","删除成功");
        //删除成功后，跳转到页面blog-input页面
        return REDIRECT_LIST;

    }


}
